//
//  CPContentTextView.m
//  ChinaPoem
//
//  Created by 赵俊明 on 2019/3/4.
//  Copyright © 2019 赵俊明. All rights reserved.
//

#import "CPContentTextView.h"

@implementation CPContentTextView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.editable = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        UILongPressGestureRecognizer *lpGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(cp_handleTap:)];
        lpGr.minimumPressDuration = 1.0;
        [self addGestureRecognizer:lpGr];
    }
    return self;
}

// 设置诗词注释文本格式
- (void)cp_explanationText:(NSString *)text fontSize:(CGFloat)size;
{
    if (!text || (text.length < 2)) {text = @"暂无解释！";}
    self.attributedText = [self cpMuAttstring:text size:size];
    
}

// 设置正文文本格式
- (void)cp_contentText:(NSString *)text fontSize:(CGFloat)size
{
    NSMutableAttributedString *aString = [self cpMuAttstring:text size:size];
    NSArray *array = [text componentsSeparatedByString:@"\n"];
    NSString *auther;
    if (array.count>1) {auther = array[1];}
    NSRange range = [text rangeOfString:auther];
    [aString addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:range];
    [aString addAttribute:NSFontAttributeName value:kFont(17.0) range:range];
    self.attributedText = aString;
}

- (NSMutableAttributedString *)cpMuAttstring:(NSString *)text size:(CGFloat)size
{
    NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
    pStyle.lineSpacing = 5;
    pStyle.firstLineHeadIndent = 0;
    pStyle.lineBreakMode = NSLineBreakByCharWrapping;
    pStyle.hyphenationFactor = 1.0;
    pStyle.paragraphSpacingBefore = 0.0;
    pStyle.paragraphSpacing = 10;
    pStyle.headIndent = 0;
    pStyle.tailIndent = 0;
    NSDictionary *aDic = @{
                           NSFontAttributeName:kFont(size),
                           NSParagraphStyleAttributeName:pStyle,
                           NSForegroundColorAttributeName:[UIColor blackColor]};
    NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:text attributes:aDic];
    return aString;
}

// 长按选中文本内容
- (void)cp_handleTap:(UILongPressGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {[self selectAll:self];}
}

@end

@implementation CPNoteView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)setTitles:(NSArray *)titles
{
    _titles = titles;
    int i = 0;
    for (NSString *name in titles) {
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitle:name forState:(UIControlStateNormal)];
        [btn setTintColor:[UIColor grayColor]];
        [btn addTarget:self action:@selector(cp_btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:btn];
        btn.tag = i + 200;
        i ++;
    }
}

- (void)cp_btnAction:(UIButton *)sender
{
    if (sender.tag < 200+_titles.count) {
        if (self.poemSource) {self.poemSource(sender.tag);}
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    int i = 0;
    CGFloat m = 6;
    CGFloat h = (self.height-(self.subviews.count+1)*m)/5;
    CGFloat w = h;
    for (UIButton *view in self.subviews) {
        view.frame = CGRectMake(0, (h+m)*i+m, w, h);
        view.layer.cornerRadius = h/2;
        view.clipsToBounds = YES;
        i ++;
    }
}
@end
